package com.milepost.workflow.config.cas;

import org.jasig.cas.client.authentication.AuthenticationFilter;
import org.jasig.cas.client.session.SingleSignOutFilter;
import org.jasig.cas.client.session.SingleSignOutHttpSessionListener;
import org.jasig.cas.client.util.AssertionThreadLocalFilter;
import org.jasig.cas.client.util.HttpServletRequestWrapperFilter;
import org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CASAutoConfiguration {

    @Autowired
    private CASAutoConfig casAutoConfig;

    private static boolean casEnabled = true;

    public CASAutoConfiguration() {
    }

    @Bean
    public ServletListenerRegistrationBean<SingleSignOutHttpSessionListener> singleSignOutHttpSessionListener() {
        ServletListenerRegistrationBean<SingleSignOutHttpSessionListener> listener = new ServletListenerRegistrationBean<SingleSignOutHttpSessionListener>();
        listener.setEnabled(casEnabled);
        listener.setListener(new SingleSignOutHttpSessionListener());
        listener.setOrder(1);
        return listener;
    }

    @Bean
    public FilterRegistrationBean singleSignOutFilter() {
        FilterRegistrationBean filterRegistration = new FilterRegistrationBean();
        filterRegistration.setFilter(new SingleSignOutFilter());
        filterRegistration.setEnabled(casEnabled);
        if(casAutoConfig.getSignOutFilters().size()>0)
            filterRegistration.setUrlPatterns(casAutoConfig.getSignOutFilters());
        else
            filterRegistration.addUrlPatterns("/*");
        filterRegistration.addInitParameter("casServerUrlPrefix", casAutoConfig.getCasServerUrlPrefix());
        filterRegistration.setOrder(2);
        return filterRegistration;
    }

    @Bean
    public FilterRegistrationBean authenticationFilter() {
        FilterRegistrationBean filterRegistration = new FilterRegistrationBean();
        filterRegistration.setFilter(new AuthenticationFilter());
        filterRegistration.setEnabled(casEnabled);
        if(casAutoConfig.getAuthFilters().size()>0)
            filterRegistration.setUrlPatterns(casAutoConfig.getAuthFilters());
        else
            filterRegistration.addUrlPatterns("/*");
        filterRegistration.addInitParameter("casServerLoginUrl", casAutoConfig.getCasServerLoginUrl());
        filterRegistration.addInitParameter("serverName", casAutoConfig.getServerName());
        filterRegistration.addInitParameter("useSession", casAutoConfig.isUseSession()?"true":"false");
        filterRegistration.addInitParameter("redirectAfterValidation", casAutoConfig.isRedirectAfterValidation()?"true":"false");
        filterRegistration.setOrder(3);
        return filterRegistration;
    }

    @Bean
    public FilterRegistrationBean cas20ProxyReceivingTicketValidationFilter() {
        FilterRegistrationBean filterRegistration = new FilterRegistrationBean();
        filterRegistration.setFilter(new Cas20ProxyReceivingTicketValidationFilter());
        filterRegistration.setEnabled(casEnabled);
        if(casAutoConfig.getValidateFilters().size()>0)
            filterRegistration.setUrlPatterns(casAutoConfig.getValidateFilters());
        else
            filterRegistration.addUrlPatterns("/*");
        filterRegistration.addInitParameter("casServerUrlPrefix", casAutoConfig.getCasServerUrlPrefix());
        filterRegistration.addInitParameter("serverName", casAutoConfig.getServerName());
        filterRegistration.addInitParameter("encoding", casAutoConfig.getEncoding());
        filterRegistration.setOrder(4);
        return filterRegistration;
    }

    @Bean
    public FilterRegistrationBean httpServletRequestWrapperFilter() {
        FilterRegistrationBean filterRegistration = new FilterRegistrationBean();
        filterRegistration.setFilter(new HttpServletRequestWrapperFilter());
        filterRegistration.setEnabled(true);
        if(casAutoConfig.getRequestWrapperFilters().size()>0)
            filterRegistration.setUrlPatterns(casAutoConfig.getRequestWrapperFilters());
        else
            filterRegistration.addUrlPatterns("/*");
        filterRegistration.setOrder(5);
        return filterRegistration;
    }

    @Bean
    public FilterRegistrationBean assertionThreadLocalFilter() {
        FilterRegistrationBean filterRegistration = new FilterRegistrationBean();
        filterRegistration.setFilter(new AssertionThreadLocalFilter());
        filterRegistration.setEnabled(true);
        if(casAutoConfig.getAssertionFilters().size()>0)
            filterRegistration.setUrlPatterns(casAutoConfig.getAssertionFilters());
        else
            filterRegistration.addUrlPatterns("/*");
        filterRegistration.setOrder(6);
        return filterRegistration;
    }
}
